﻿using System.Collections;
using Entity.Hibernate;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System;

namespace Bussines
{
    public class Service<T>
    {
        readonly System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
        System.Configuration.ConnectionStringSettings connString;

        public readonly BaseHibernateDao<T, int> dao = new BaseHibernateDao<T, int>("hibernate.cfg.xml");

        public Service()
        {
            connString = rootWebConfig.ConnectionStrings.ConnectionStrings["BichThuyGasConnectString"];
        }

        public List<T> SelectAll()
        {
            return dao.GetAll();
        }

        public T Insert(T obj)
        {
            dao.Save(obj, false);
            return obj;
        }
        public T Select(int id)
        {
            return dao.GetById(id, true);
        }

        public List<T> Select(String column, String value)
        {
            var searchCriteria = new SearchCriteria(typeof(T));
            var searchObj = new SearchObj(column, value);
            searchCriteria.addSearchItem(searchObj);
            return dao.GetByCriteria(searchCriteria);
        }

        public void Delete(T obj)
        {
            dao.Delete(obj);
        }

        public void Update(T obj)
        {
            dao.UpDate(obj);
        }
        public List<T> Search(Hashtable hashtable)
        {
            SearchCriteria searchCriteria = new SearchCriteria(typeof(T));
            SearchObj searchObjhang;
            foreach (DictionaryEntry item in hashtable)
            {
                searchObjhang = new SearchObj(item.Key.ToString(), item.Value);
                searchObjhang.Oper = SearchObj.OP_LIKE;
                searchCriteria.addSearchItem(searchObjhang);
            }
            List<T> list = new List<T>();
            list = dao.GetByCriteria(searchCriteria);
            return list;
        }
    }
}